package com.woniuxy.service.impl;

import com.woniuxy.entity.MFans;
import com.woniuxy.entity.MFansExample;
import com.woniuxy.entity.MUser;
import com.woniuxy.entity.MUserExample;
import com.woniuxy.mapper.MFansMapper;
import com.woniuxy.mapper.MUserMapper;
import com.woniuxy.service.FansService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FansServiceImpl implements FansService {
    @Autowired
    MUserMapper usermapper;
    @Autowired
    MFansMapper fansMapper;
    @Override
    public List<MUser> searchFans(Integer id) {
        MFansExample mFansExample = new MFansExample();
        mFansExample.or().andUidEqualTo(Long.valueOf(id));
        List<MFans> mFans = fansMapper.selectByExample(mFansExample);

        List<Long> mFansId = new ArrayList<>();
        for (MFans fans:mFans) {
            mFansId.add(fans.getFansid());
        }

        MUserExample mUserExample = new MUserExample();
        mUserExample.or().andIdIn(mFansId);
        List<MUser> mUsers = usermapper.selectByExample(mUserExample);
        return mUsers;
    }

    @Override
    public List<MUser> searchFollow(Integer id) {
        MFansExample mFansExample = new MFansExample();
        mFansExample.or().andFansidEqualTo(Long.valueOf(id));
        List<MFans> mFollows = fansMapper.selectByExample(mFansExample);

        List<Long> mFollowsId = new ArrayList<>();
        for (MFans fans:mFollows) {
            mFollowsId.add(fans.getUid());
        }

        MUserExample mUserExample = new MUserExample();
        mUserExample.or().andIdIn(mFollowsId);
        List<MUser> mUsers = usermapper.selectByExample(mUserExample);
        return mUsers;
    }

    @Override
    public int unFollow(Integer id, Integer loginId) {
        MFansExample mFansExample = new MFansExample();
        mFansExample.or().andFansidEqualTo(Long.valueOf(loginId)).andUidEqualTo(Long.valueOf(id));
        return fansMapper.deleteByExample(mFansExample);
    }

    @Override
    public Map<Long, List<Long>> searchAllUserWithFriends(List<Integer> mFollowsId) {
        Map<Long, List<Long>> All = new HashMap<>();

        for (Integer id : mFollowsId){
            List<Long> lFans = new ArrayList<>();
            List<Integer> fans = fansMapper.searchFansId(id);
            for (Integer i : fans) {
                lFans.add(Long.valueOf(i));
            }
            All.put(Long.valueOf(id),lFans);
        }


        return All;
    }
}
